﻿using System.Collections.Generic;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository.Base;
using System.Repository.DataBase.Models;
using Microsoft.EntityFrameworkCore;

namespace System.Repository
{

    /// <summary>
    ///组织表 接口 仓库实现 
    /// </summary>	
    public class SysOrganizeRepository : RepositoryDomain<SysOrganizeEntity, Sys_Organize>, ISysOrganizeRepository
    {
        public SysOrganizeRepository(IDbContextConfig config) : base(config)
        {
        }
        public override bool Update(SysOrganizeEntity entity)
        {
            Sys_Organize model = GetModel(entity.Id);
            if (model == null)
                return false;
            Dbcontext.Set<Sys_Organize>().Attach(model);
            Dbcontext.Entry(model).State = EntityState.Unchanged;
            AutoCopy(model, entity);
            return Dbcontext.SaveChanges() > 0;
        }

        public override bool Delete(string key)
        {
            return Delete(t => t.Id == key) > 0;
        }

        public List<SysOrganizeEntity> GetParentIdList(SysOrganizeTypeEnum typeEnum, string parentId)
        {
            //  throw new System.NotImplementedException();

            List<Sys_Organize> data = new List<Sys_Organize>();
            RecursionTree(ref data, typeEnum, parentId, 0);//递归查询 
            List<SysOrganizeEntity> list = new List<SysOrganizeEntity>();
            return GetList(list, data);
        }

        /// <summary>
        /// 通过递归方式获取 子元素
        /// </summary>
        /// <param name="treeModel"></param>
        /// <param name="typeEnum"></param>
        /// <param name="parentId"></param>
        /// <param name="layer"></param>
        public void RecursionTree(ref List<Sys_Organize> treeModel, SysOrganizeTypeEnum typeEnum, string parentId, int layer)
        {
            if (!string.IsNullOrEmpty(parentId))
            {
                var expression = ExtLinq.True<Sys_Organize>();
                expression = expression.And(t => t.ParentId == parentId).And(t => t.CategoryId == typeEnum.ToString());
                List<Sys_Organize> temp = IQueryable(expression).ToList();

                foreach (var item in temp)
                {
                    if (layer == 0)
                    {
                        item.ParentId = "0";
                    }
                    treeModel.Add(item);
                    RecursionTree(ref treeModel, typeEnum, item.Id, layer + 1);
                }

            }
        }


        /// <summary>
        /// 通过父级 id 查找
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<SysOrganizeEntity> GetParentIdList(string parentId)
        {
            List<SysOrganizeEntity> list = new List<SysOrganizeEntity>();
            var expression = ExtLinq.True<Sys_Organize>();
            expression = expression.And(t => t.ParentId.Contains(parentId));
            List<Sys_Organize> temp = IQueryable(expression).ToList();
            return GetList(list, temp);
        }

    
    }

}
